package com.bwie.interrogation.service.impl;

import com.bwie.common.security.utils.SecurityUtils;
import com.bwie.interrogation.domain.AddressData;
import com.bwie.interrogation.domain.dto.AddAddressDTO;
import com.bwie.interrogation.domain.vo.AddressVo;
import com.bwie.interrogation.mapper.InterrogationMapper;
import com.bwie.interrogation.service.InterrogationService;
import com.bwie.interrogation.util.AddressRecognitionUtil;
import com.bwie.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InterrogationServiceImpl implements InterrogationService {
    /**
     * 注入
     */
    @Autowired
    private InterrogationMapper mapper;

    /**
     * 患者添加地址
     * @param addAddressDTO
     */
    @Override
    public void addAddress(AddAddressDTO addAddressDTO) {
        //获取登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //获得地址详情
        AddressData addressData = AddressRecognitionUtil.AddressRecognition(addAddressDTO.getAddress());
        //患者添加地址
        int i = mapper.addPatientAddress(loginUser.getUserid(),addAddressDTO,addressData.getData());
        //判断是否添加成功
        if (i<=0){
            //抛异常
            throw new RuntimeException("添加失败");
        }
    }

    /**
     * 获得患者地址列表
     * @return
     */
    @Override
    public List<AddressVo> getAddressList() {
        //获取登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //根据登录用户查询地址
        List<AddressVo> list = mapper.getAddressList(loginUser.getUserid());
        //返回
        return list;
    }

    /**
     * 患者删除地址
     * @param id
     */
    @Override
    public void delAddress(Long id) {
        //患者删除地址
        int i = mapper.delAddress(id);
        //判断是否删除成功
        if (i<=0){
            throw new RuntimeException("删除失败");
        }
    }

    /**
     * 患者获得医生的名称
     * @return
     */
    @Override
    public String getDoctorName() {
        //获取登录用户的id
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //根据id查询挂号的医生名称
        String doctorName = mapper.getDoctorName(loginUser.getUserid());
        //返回
        return doctorName;
    }
}
